/**
 * 1.字符数组&字符串 https://www.bilibili.com/video/BV1vs411n7TH?p=49
 * C 语言中没有字符串这种数据类型，可以通过 char 的数组来替代;
 * 字符串一定是一个 char 的数组，但 char 的数组未必是字符串; (char数组以0结尾, 才是字符串).
 * 数字 0(和字符“\0’等价)结尾的 char 数组就是一个字符串，但如果 char数组没有以数字 0 结尾，那么就不是一个字符串，只是普通字符数组;
 * 所以字符串是一种特殊的 char 的数组。
 */
#include <stdio.h>

int main() {
    //1.字符数组
//                                     打印结果  sizeof             备注
    char a[2] = {65};                  //A      2(不写[2]默认1)     最后要留一位给 '\0', 否则 printf会一直输出直到 != 0
    char b[100] = {'a', 'b', 'c', 'd'};//abcd   100
    char c[5]   = {'a', 'b', 'c', 'd'};//abcd   5(不写[5]默认4)     最后要留一位给 '\0'
    char d1[]    = {};                 //       0
    printf("a = %s, sizeof = %d\n", a, sizeof(a));
    printf("b = %s, sizeof = %d\n", b, sizeof(b));
    printf("c = %s, sizeof = %d\n", c, sizeof(c));


    //2.数组字符串
    char d[100] = "hello";             //hello   100
    char e[] = "world";                //world   6                 sizeof自动+1, 最后1位以 '\0' 结尾
    char f[] = {"abcd"};               //abcd    5                 sizeof自动+1
    printf("d = %s, sizeof = %d\n", d, sizeof(d));
    printf("e = %s, sizeof = %d\n", e, sizeof(e));
    printf("f = %s, sizeof = %d\n", f, sizeof(f));


    //3.字符串数组
    char g[][3] = {"ab", "cd"};        //因为是数组, 所以 ab长度2+1 = 3, 等价于 g[2][3]
    printf("g = %s %s, sizeof = %d\n", g[0], g[1], sizeof(g));//ab cd, sizeof = 6


    //4.键盘输入字符数组
    char aa[10];
    scanf("%s", aa);    //aa就是地址, 所以不用写&aa,  键盘输入字符串长度≤9, 否则报错(为什么我的CLion不报错?)
    printf("aa = %s, sizeof = %d\n", aa, sizeof(aa));   //aa = 撒地方, sizeof = 10


    //5.字符串拼接
    char bb[11];
    int i = 0, j = 0;
    while (d[i]) {          //和 (d[i] != '\0') 一样效果
//        bb[i++] = d[i];   //不能这样写, 否则i值不对: Unsequenced modification and access to 'i'
        bb[i] = d[i];
        i++;
    }
    while (e[j]) {
        bb[i + j] = e[j];
        j++;
    }
    bb[i + j] = 0;       //0 就表示'\0', 在结尾要拼接上 '\0'
    printf("bb = %s, sizeof = %d\n", bb, sizeof(bb));//bb = helloworld, sizeof = 11

    return 0;
}
